package Android.Billarm;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DBISODate extends GregorianCalendar {

	private static final long serialVersionUID = 1L;
	
	//creates a lenient calendar with current date 
	public DBISODate() {
		super();
		//lenient for example if date is 32 it will be turned to 1 the next month
		this.setLenient(true);
	}
	
	//creates a lenient calendar with specified arguments
	public DBISODate(int year, int month, int day, int hour, int minute) {
		super(year, month, day, hour, minute);
		//lenient for example if date is 32 it will be turned to 1 the next month
		this.setLenient(true);
	}

	//creates a lenient calendar using an isoDate format
	//isoDate represented as ISO8601 string ("YYYY-MM-DD HH:MM")
	public DBISODate(String isoDate) {
		super(Integer.parseInt(isoDate.substring(0, 4)),
				Integer.parseInt(isoDate.substring(5, 7)),
				Integer.parseInt(isoDate.substring(8, 10)),
				Integer.parseInt(isoDate.substring(11, 13)),
				Integer.parseInt(isoDate.substring(14, 16)));
		//lenient for example if date is 32 it will be turned to 1 the next month
		this.setLenient(true);
	}
	
	//returns date represented as ISO8601 string ("YYYY-MM-DD HH:MM")
	public String getISODate() {
		String year = String.valueOf(get(Calendar.YEAR));
		String month = String.valueOf(get(Calendar.MONTH));
		String day = String.valueOf(get(Calendar.DAY_OF_MONTH));
		String hour = String.valueOf(get(Calendar.HOUR_OF_DAY));
		String minute = String.valueOf(get(Calendar.MINUTE));
		
		//match formating as ISO8601
		while(year.length() < 4)
			year = "0" + year;
		if(month.length() < 2)
			month = "0" + month;
		if(day.length() < 2)
			day = "0" + day;
		if(hour.length() < 2)
			hour = "0" + hour;
		if(minute.length() < 2)
			minute = "0" + minute;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute;
	}
	//returns true if date successfully changed
	//returns false if date wasn't changed
	public boolean setISODate(String isoDate){
		set(Integer.parseInt(isoDate.substring(0, 4)),
				Integer.parseInt(isoDate.substring(5, 7)),
				Integer.parseInt(isoDate.substring(8, 10)),
				Integer.parseInt(isoDate.substring(11, 13)),
				Integer.parseInt(isoDate.substring(14, 16)));
		return true;
	}

    public boolean setTimeOfDay (int hour, int minute)
    {
        set (HOUR_OF_DAY, hour);
        set (MINUTE, minute);
        return true;
    }
}
